package com.banciyuan.bcywebview.biz.search;

import android.content.Context;
import android.content.Intent;
import android.text.Html;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.base.factory.TimelineFactory;
import com.banciyuan.bcywebview.base.helper.data.UserDataHelper;
import com.banciyuan.bcywebview.base.helper.timelinedatacenter.TimelineLikeHelper;
import com.banciyuan.bcywebview.base.view.imageview.CircleImageView;
import com.banciyuan.bcywebview.base.view.toast.MyToast;
import com.banciyuan.bcywebview.biz.detail.post.PostDetailActivity;
import com.banciyuan.bcywebview.biz.pc.zone.smooth.SmoothPersonActivity;
import com.banciyuan.bcywebview.utils.constants.MyConstants;
import com.banciyuan.bcywebview.utils.intent.gotoUtil;
import com.banciyuan.bcywebview.utils.string.StringUtil;
import com.banciyuan.bcywebview.utils.universalimageloader.core.ImageLoader;
import com.banciyuan.bcywebview.utils.universalimageloader.utils.ImageOptions;

import java.util.List;

import de.greenrobot.daoexample.model.SearchContent;


public class SearchComplexAdapter extends BaseAdapter {

    private Context mContext;
    private LayoutInflater inflater;
    private List<SearchContent> slist;
    private ImageLoader imageLoader;
    private final int POST_TYPE = 0;
    private final int POST_TYPE_WITH_PIC = 3;
    private final int TITLE_TYPE = 1;
    private final int USER_TYPE = 2;

    public SearchComplexAdapter(List<SearchContent> slist, Context mContext) {
        imageLoader = ImageLoader.getInstance();
        this.slist = slist;
        this.mContext = mContext;
        this.inflater = LayoutInflater.from(this.mContext);
    }

    @Override
    public int getCount() {
        return slist.size();
    }

    @Override
    public Object getItem(int i) {
        return slist.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public int getViewTypeCount() {
        return 4;
    }

    @Override
    public int getItemViewType(int position) {
        switch (TimelineFactory.whichType(slist.get(position))) {
            case TimelineFactory.TIMELINE_TYPE_GROUP:
                if (slist.get(position).getMulti() != null && !slist.get(position).getMulti().isEmpty()) {
                    return POST_TYPE_WITH_PIC;
                } else {
                    return POST_TYPE;
                }
            default:
                String otype = slist.get(position).getOtype();
                if (!TextUtils.isEmpty(otype)) {
                    if (otype.equals("user")) {
                        return USER_TYPE;
                    }
                }
        }
        return TITLE_TYPE;
    }

    private class TitleViewHolder {
        private TextView title;

        public TitleViewHolder(View convertView) {
            title = (TextView) convertView.findViewById(R.id.ground_zan_type_tv);
        }
    }

    private class GroupPostHolder {

        private TextView post_title;
        private TextView post_intro;
        private ImageView post_iv;
        private RelativeLayout container;

        public GroupPostHolder(View convertView) {
            post_title = (TextView) convertView.findViewById(R.id.group_new_item_title_one);
            post_intro = (TextView) convertView.findViewById(R.id.talk_intro);
            post_iv = (ImageView) convertView.findViewById(R.id.best_img);
            container = (RelativeLayout) convertView.findViewById(R.id.group_new_line_one);
        }
    }

    private class UserViewHolder {
        private CircleImageView img;
        private CircleImageView sex;
        private TextView name;
        private TextView job;
        private TextView intro;
        private TextView follow;
        private View container;

        public UserViewHolder(View convertView) {
            img = (CircleImageView) convertView.findViewById(R.id.search_img);
            sex = (CircleImageView) convertView.findViewById(R.id.search_person_sex);
            name = (TextView) convertView.findViewById(R.id.search_title);
            job = (TextView) convertView.findViewById(R.id.search_work);
            intro = (TextView) convertView.findViewById(R.id.search_tags);
            follow = (TextView) convertView.findViewById(R.id.teams_recommend_img);
            container = convertView.findViewById(R.id.container);
        }
    }


    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        GroupPostHolder groupPostHolderPic = null;
        GroupPostHolder groupPostHolder = null;
        TitleViewHolder titleViewHolder = null;
        UserViewHolder userViewHolder = null;

        if (view == null) {
            switch (getItemViewType(i)) {
                case POST_TYPE:
                    view = inflater.inflate(R.layout.group_new_item, viewGroup, false);
                    groupPostHolder = new GroupPostHolder(view);
                    view.setTag(groupPostHolder);
                    break;
                case POST_TYPE_WITH_PIC:
                    view = inflater.inflate(R.layout.group_new_item_with_pic, viewGroup, false);
                    groupPostHolderPic = new GroupPostHolder(view);
                    view.setTag(groupPostHolderPic);
                    break;
                case USER_TYPE:
                    view = inflater.inflate(R.layout.search_user_item, viewGroup, false);
                    userViewHolder = new UserViewHolder(view);
                    view.setTag(userViewHolder);
                    break;
                default:
                case TITLE_TYPE:
                    view = inflater.inflate(R.layout.search_result_num, viewGroup, false);
                    titleViewHolder = new TitleViewHolder(view);
                    view.setTag(titleViewHolder);
                    break;
            }
        } else {
            switch (getItemViewType(i)) {
                case POST_TYPE:
                    groupPostHolder = (GroupPostHolder) view.getTag();
                    break;
                case POST_TYPE_WITH_PIC:
                    groupPostHolderPic = (GroupPostHolder) view.getTag();
                    break;
                case USER_TYPE:
                    userViewHolder = (UserViewHolder) view.getTag();
                    break;
                default:
                case TITLE_TYPE:
                    titleViewHolder = (TitleViewHolder) view.getTag();
                    break;
            }
        }

        switch (getItemViewType(i)) {
            case POST_TYPE:
                setGroupPost(groupPostHolder, slist.get(i));
                break;
            case POST_TYPE_WITH_PIC:
                setGroupPost(groupPostHolderPic, slist.get(i));
                break;
            case USER_TYPE:
                setUserUI(userViewHolder, slist.get(i), i);
                break;
            case TITLE_TYPE:
                setTitleUI(titleViewHolder, slist.get(i));
                break;
            default:
                break;
        }

        return view;
    }

    private void setGroupPost(GroupPostHolder groupPostHolder, final SearchContent searchContent) {
        if (searchContent.getMulti() != null && !searchContent.getMulti().isEmpty() && searchContent.getMulti().size() > 0) {
            ImageLoader.getInstance().displayImage(searchContent.getMulti().get(0).getPath(), groupPostHolder.post_iv, ImageOptions.options);
        }
        if (!TextUtils.isEmpty(searchContent.getGroup().getName())) {
            groupPostHolder.post_title.setText(Html.fromHtml(searchContent.getGroup().getName()));
        }
        if (!TextUtils.isEmpty(searchContent.getPlain())) {
            groupPostHolder.post_intro.setText(Html.fromHtml(searchContent.getPlain()));
        }
        groupPostHolder.container.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, PostDetailActivity.class);
                intent.putExtra(MyConstants.POSTIDVALUE, searchContent.getPost_id());
                intent.putExtra(MyConstants.GIDVALUE, searchContent.getGroup().getGid());
                intent.putExtra(MyConstants.TIMELINETYPE, TimelineFactory.TIMELINE_TYPE_GROUP);
                mContext.startActivity(intent);
            }
        });
    }

    private void setUserUI(UserViewHolder userViewHolder, final SearchContent searchContent, final int postion) {
        if (!TextUtils.isEmpty(searchContent.getAvatar())) {
            imageLoader.displayImage(searchContent.getAvatar(), userViewHolder.img, ImageOptions.optionHead);
        }
        if (!TextUtils.isEmpty(searchContent.getUname())) {
            userViewHolder.name.setText(searchContent.getUname());
        }
        if (searchContent.getSex() == 1) {
            userViewHolder.sex.setImageResource(R.drawable.profile_boy);
        } else {
            userViewHolder.sex.setImageResource(R.drawable.profile_girl);
        }
        if (searchContent.getFollowstate().equals("unfollow")) {
            if (StringUtil.notNullEqual(UserDataHelper.getInstance(mContext).getUid(), searchContent.getUid())) {
                userViewHolder.follow.setVisibility(View.GONE);
            } else {
                userViewHolder.follow.setVisibility(View.VISIBLE);
            }
        } else {
            userViewHolder.follow.setVisibility(View.GONE);
        }
        String roles = "";
        for (int i = 0; i < searchContent.getRoles().size(); i++) {
            if (i == searchContent.getRoles().size() - 1) {
                roles += searchContent.getRoles().get(i).getType_name();
            } else {
                roles += searchContent.getRoles().get(i).getType_name() + " / ";
            }
        }
        if (!TextUtils.isEmpty(roles)) {
            userViewHolder.job.setVisibility(View.VISIBLE);
            userViewHolder.job.setText(roles);
        } else {
            userViewHolder.job.setVisibility(View.GONE);
        }

        if (!TextUtils.isEmpty(searchContent.getSelf_intro())) {
            userViewHolder.intro.setVisibility(View.VISIBLE);
            userViewHolder.intro.setText(Html.fromHtml(searchContent.getSelf_intro()));
        } else {
            userViewHolder.intro.setVisibility(View.GONE);
        }

        userViewHolder.container.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                gotoUtil.gotoAct(mContext, SmoothPersonActivity.class, searchContent.getUid());
            }
        });

        userViewHolder.follow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dofollow(searchContent.getUid(), postion, view);
            }
        });
    }

    private void setTitleUI(TitleViewHolder titleViewHolder, SearchContent searchContent) {
        if (!TextUtils.isEmpty(searchContent.getTitle_num_format())) {
            titleViewHolder.title.setText(Html.fromHtml(searchContent.getTitle_num_format()));
        }
    }

    public void dofollow(final String uid, final int postion, final View view) {
        TimelineLikeHelper.doFollow(mContext, "dofollow", uid, new TimelineLikeHelper.FollowSuccess() {
            @Override
            public void onFollowSuccess(String response) {
                MyToast.show(mContext, mContext.getString(R.string.focus_succ));
                slist.get(postion).setFollowstate("havafollow")  ;
                view.setVisibility(View.GONE);
            }
        });
    }
}
